﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace customized_keyboard
{

    
    public partial class getKeyCode : Form
    {
        public string keycodestr;

        MyDictionary myDictionary1 = new MyDictionary();

        public getKeyCode()
        {
            InitializeComponent();
            textBox1.Text = "";
            comboBox1.Text = "请选择按键功能";
            foreach (var boxvalue in myDictionary1.MEDIA_KEY_DICT.Keys) { comboBox1.Items.Add(boxvalue); }

            foreach (var kvp in myDictionary1.NORMAL_KEY_DICT)
            {
                int index = dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = kvp.Key;
                dataGridView1.Rows[index].Cells[1].Value = "0x" + Convert.ToString(kvp.Value, 16);
            }
            dataGridView1.Show();

            //dataGridView1.DataSource = myDictionary1.ToList();
        }
        public string Keycodestr
        {
            get { return this.keycodestr; }
            //set { keycodestr = textBox1.Text; }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            textBox2.Text = "";
            if (!string.IsNullOrEmpty(textBox1.Text)) textBox1.Text += "+";
            textBox1.Text += e.KeyCode.ToString();

        }

        private void getKeyCode_FormClosing(object sender, FormClosingEventArgs e)
        {
            //this.DialogResult = DialogResult.OK;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (string.IsNullOrEmpty(textBox1.Text)) {
                MessageBox.Show("请输入或者关闭!");
                return;
            }
            var num = 0;
            string[] resultString = textBox1.Text.Split(new Char[] { '+'}, StringSplitOptions.RemoveEmptyEntries);
            foreach(var str in resultString)
            {
                if (!myDictionary1.NORMAL_KEY_DICT.ContainsKey(str)) // True 
                {
                    MessageBox.Show("输入按键有问题，请参照右边表格输入修改!");
                    return;
                }
                num++;
            }
            if (num > 93)
            {
                MessageBox.Show("输入按键过多!");
                return;
            }
            keycodestr = 'N' + textBox1.Text;
            this.DialogResult = DialogResult.OK;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            keycodestr = 'M' + comboBox1.SelectedItem.ToString();
            //Console.WriteLine(comboBox1.SelectedIndex);
            this.DialogResult = DialogResult.OK;
        }
    }
}
